/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package codegen;

/**
 *
 * @author Felipe Gutierrez
 */


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.StringTokenizer;


import xmibase.XmiClass;

public class FilesParser {

	private Reader reader;
	private String outDir;

	/**
	 * Constructs a new FilesParser with a XML file.
	 *
	 * @param xmlString
	 */
	public FilesParser(String outDir) {
		this.outDir = outDir;
	}

	public FilesParser() {

	}


        //Metoddo criado por Felipe Gutierrez

        public void generateEntityFiles(String fileName , String content , String module_visu )throws IOException {


		if (outDir == null) {
			throw new FileNotFoundException("URL is null");
		}

		File dir;
		dir = new File(outDir);
		if (!dir.exists()) {
			throw new IOException("Directory does not exists.(" + dir.getPath()
					+ ")");
		}
		if (!dir.isDirectory()) {
			throw new IOException("Input parameter must be a directory.("
					+ dir.getPath() + ")");
		}


		int count = 0;



		// create the source directories and a String with the new path
		// of the out directory
		String directoryPath = dir.getPath();

			directoryPath = this.createDirectoriesEntity(dir,module_visu);


		// create the directories of the module
		//createDirectories(directoryPath, pack);

		
		// path of the file
		String fname = directoryPath + "\\" + fileName;//+ File.separator + pack + File.separator + fileName;

		FileWriter fw = new FileWriter(fname);

		fw.write(content);
		fw.close();





        }

	public void generateFiles(String fileName, String module_PKG, String module_PKG_s,String module_visu ,String appName,
			String content) throws IOException {

        String subPackage = module_PKG_s;
		if (outDir == null) {
			throw new FileNotFoundException("URL is null");
		}

		File dir;
		dir = new File(outDir);
		if (!dir.exists()) {
			throw new IOException("Directory does not exists.(" + dir.getPath()
					+ ")");
		}
		if (!dir.isDirectory()) {
			throw new IOException("Input parameter must be a directory.("
					+ dir.getPath() + ")");
		}/*
		String subpackage;
		if ( !(template.getNameSubPackage().isEmpty()) ) {
			subpackage = template.getNameSubPackage();
		} else {
			subpackage ="";
		}*/

		// generate files

		int count = 0;
//		if( appName == null || appName.equals("") ) {
//			String[] aux = module_PKG.split("\\p{Punct}");
//			appName = aux[0];
//		}
		//String module = module;

		// getModule returns a Punct at the end!
		String pack = module_PKG;

		// create the source directories and a String with the new path
		// of the out directory
		String directoryPath = dir.getPath();
		if ((pack != null) && (!pack.equals(""))) {
			directoryPath = createDirectoriesSRC(dir,module_visu, pack,subPackage, appName);
		}

		// create the directories of the module
		//createDirectories(directoryPath, pack);

		pack = pack.replace('.', File.separator.charAt(0));
		// path of the file
		String fname = directoryPath + "\\" + fileName;//+ File.separator + pack + File.separator + fileName;

		FileWriter fw = new FileWriter(fname);

		fw.write(content);
		fw.close();

	}


        //Metodo criado por Felipe Gutierrez
	public String createDirectoriesEntity(File dir , String visuPack){

            String newRoot = dir.getAbsolutePath();

		// split the modules
		//String[] auxModule = pack.split("\\p{Punct}");

		// set the Directories SRC
		String[] directoriesSRC = {visuPack};
        //String[] directoriesSRC = { " src "};
		for (int i = 0; i < directoriesSRC.length ; i++) {


            File auxDir = new File(newRoot + "\\" + directoriesSRC[i]);
            if (!auxDir.exists()) {
				auxDir.mkdir();
			}
            newRoot = newRoot + "\\" + directoriesSRC[i];

            }


			//newRoot += directoriesSRC[i] + File.separator;

		return newRoot;


        }

        //Metodo alterado anterirmente, modificar pelo original
	public String createDirectoriesSRC(File dir,String visuPack, String pack,String subPackage, String appName) {
		String newRoot = dir.getAbsolutePath();

		// split the modules
		//String[] auxModule = pack.split("\\p{Punct}");

		// set the Directories SRC
		String[] directoriesSRC = {"com" , visuPack , pack , subPackage , "backbean"};
        //String[] directoriesSRC = { " src "};
		for (int i = 0; i < directoriesSRC.length ; i++) {
			
            
            File auxDir = new File(newRoot + "\\" + directoriesSRC[i]);
            if (!auxDir.exists()) {
				auxDir.mkdir();
			}
            newRoot = newRoot + "\\" + directoriesSRC[i];
            
            }
			

			//newRoot += directoriesSRC[i] + File.separator;
        
		return newRoot;
	}

	/**
	 * Create the directories and sub-directories
	 *
	 * @param dirPath
	 *            - path where will be create the directories
	 * @param module
	 *            - String with the names of the directories that will be
	 *            created
	 */
	public void createDirectories(String dirPath, String module) {

		String auxModule = module;

		// String[] directories = auxModule.split("\\p{Punct}");
		StringTokenizer tokenizer = new StringTokenizer(auxModule, ".");
		// for (int i = 0; i < directories.length; i++) {
		while (tokenizer.hasMoreTokens()) {
			String token = tokenizer.nextToken();
			File auxDir = new File(dirPath + File.separator + token);

			if (!auxDir.exists()) {
				auxDir.mkdir();
			}

			dirPath += File.separator + token;
		
	}

}
}
